package com.vinjogames.util;

import android.graphics.PointF;
import android.util.FloatMath;

/**
 * 
 * Vector class used for all physics calculations.
 * 
 * @author Joey Jones
 *
 */
public class VectorF {
	private static VectorF temp = new VectorF();
	
	private float x;
	private float y;
	
	public VectorF() { x = 0.0f; y = 0.0f; }
	
	public VectorF(float xVal, float yVal) { x = xVal; y = yVal; }
	
	public VectorF(VectorF v) { x = v.x; y = v.y; }
	
	public VectorF(PointF p) { x = p.x; y = p.y; }
	
	public VectorF plus(VectorF v) { x += v.x; y += v.y; return this; }

	public float dot(VectorF v) { return x * v.x + y * v.y;	}
	
	public float magnitude() { return FloatMath.sqrt(x * x + y * y); }
	
	/**
	 * @return The angle between the vector and the horizontal x-axis in degrees.
	 */
	public float angle() {
		if(x == 0.0f)
			return  ((y > 0.0f ? (float)(Math.PI / 2) : (float)(Math.PI * 3 / 2))) * 180.0f / (float)(Math.PI * 2.0);
		return (float)Math.atan(y / x) * 180.0f / (float)(Math.PI * 2.0);
	}
	
	/**
	 * @returnThe angle between the vector and the horizontal x-axis in radians.
	 */
	public float angleRads() {
		if(x == 0.0f)
			return  y > 0.0f ? (float)(Math.PI / 2) : (float)(Math.PI * 3 / 2);
		return (float)Math.atan(y / x);
	}
	
	public VectorF times(float a) { x *= a; y *= a;	return this; }
	
	public PointF pointPlus(PointF p) { p.x += x; p.y += y;	return p; }

	public float getX() { return x;	}
	public void setX(float x) {	this.x = x;	}
	public float getY() { return y;	}
	public void setY(float y) {	this.y = y;	}
	
	public static VectorF aMinusB(PointF a, PointF b) {	temp.x = a.x - b.x;	temp.y = a.y - b.y;	return temp; }
}
